package com.quanyan.runningriding.web.running.Admin;


import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.validation.base.Update;
import com.quanyan.runningriding.entity.vo.run.MusicVo;
import com.quanyan.runningriding.service.RunMusicService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * RunMusicController
 *
 * @author Tu Chun
 * @date 2016/8/23
 */
@RestController
@RequestMapping(value = "/admin/api/run")
public class MusicAdminController {
    @Autowired
    private RunMusicService runMusicService;
    private Logger logger = LoggerFactory.getLogger(MusicAdminController.class);


    @RequestMapping(value = "/batchUploadMusic", method = RequestMethod.POST)
    public APIResponse<?> batchUploadMusic(@RequestBody List<MusicVo> musicVos) {
        APIResponse<?> result = runMusicService.batchUploadMusic(musicVos);
        return result;
    }

    @RequestMapping(value = "/musicList", method = RequestMethod.POST)
    public APIResponse<PageObj<List<MusicVo>>> musicList(@RequestBody Map<String, Object> paramMap) {


        Integer pageNum = MapUtils.getInteger(paramMap,"pageNum");
        Integer pageSize = MapUtils.getInteger(paramMap,"pageSize");
        String name =MapUtils.getString(paramMap,"name");
        String author = MapUtils.getString(paramMap,"author");
        Byte type = MapUtils.getByte(paramMap,"type");
        PageObj<List<MusicVo>> result = runMusicService.musicList(name, author, type, pageNum, pageSize);
        return APIResponse.returnSuccess(result);
    }

    @RequestMapping(value = "/updateMusic", method = RequestMethod.POST)
    public APIResponse<?> updateMusic(@RequestBody @Validated(Update.class) MusicVo musicVo) {
        APIResponse<?> result = runMusicService.updateMusic(musicVo);
        return result;
    }

    @RequestMapping(value = "/deleteMusic", method = RequestMethod.POST)
    public APIResponse<?> deleteMusic(@RequestBody Map<String, Object> paramMap) {
        Integer musicId = MapUtils.getInteger(paramMap,"musicId");
        APIResponse<?> result = runMusicService.deleteMusic(musicId);
        return result;
    }

}
